<template>
	<view class="login">
		<!--end 登录图标-->
		<view class="logo_wrap">
			<image class="img" src="../../static/image/public/logo.png"></image>
		</view>
		<view class="content">
			<view class="btn_wrap">
				<view class="header_tab" :class="[currentTab==0 ? 'on' : '']" :data-current="0" @click="swichNav">
					手机验证码登录
				</view>
				<view class="header_tab" :class="[currentTab==1 ? 'on' : '']" :data-current="1" @click="swichNav">
					账号密码登录
				</view>
			</view>

			<login ref="childPass" v-if="currentTab == 1"></login>
			<yzmLogin ref="childYzm" v-if="currentTab == 0"></yzmLogin>

			<wyb-button class="btn" :ripple="true" type="filled" color="#585cf7" :radius="radius" width="100%"
				height="88rpx" font-size="32rpx" @click="login">登录</wyb-button>
			
		</view>
		<navigator url="/pages/register/register" hover-class="loginBtn-hover" class="loginBtn zc-btn">注册
		</navigator>
			<!-- <view class="footer">
				<view class="footer-text">——— 其他登录方式 ———</view>
				<button bindtap="getWXLogin" hover-class="img-hover" class="btn">
					<image class="img" src="../../static/image/page/wx.png"></image>
				</button>
			</view> -->
		<wyb-loading ref="loading" title="登录中" />
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters,
		mapActions
	} from 'vuex'
	import graceChecker from '@/common/graceChecker.js'
	import formRuleConfig from '@/config/formRule.config.js'
	import AES from "@/common/AES.js";
	//获取子组件
	import login from './child/login.vue';
	import yzmLogin from './child/yzmLogin.vue';
	export default {
		data() {
			return {
				radius: ["100px"],
				currentTab: 0
			}
		},
		computed: {
			...mapState(["openId"]),
			...mapGetters(['hasLogin']),

		},
		components: {
			login,
			yzmLogin
		},
		methods: {
			//登录事件
			login() {
				//判断是密码登录还是验证码登录
				if (this.currentTab == 0) {
					this.yzmLogin();
				} else {
					this.passwordLogin();
				}
			},
			//密码登录
			passwordLogin() {
				let that = this;
				let parmas = this.$refs.childPass.login;
				//验证格式
				let checkRes = graceChecker.check(parmas, formRuleConfig.loginRule);
				if (!checkRes) {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
					return;
				}
				this.$refs.loading.showLoading();
				//开始AES加密
				let keys = getApp().globalData.key;
				let enUsername = AES.encrypt(parmas.username, keys);
				let enPassword = AES.encrypt(parmas.password, keys);
				let encrypts = {};
				encrypts.username = enUsername;
				encrypts.password = enPassword;
				//调用登录接口
				this.goToLogin(encrypts);
			},
			//验证码登录
			yzmLogin() {
				let that = this;
				let parmas = this.$refs.childYzm.login;
				//验证
				let checkRes = graceChecker.check(parmas, formRuleConfig.loginCodeRule);
				if (!checkRes) {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
					return;
				}
				this.$refs.loading.showLoading();
				//开始AES加密
				let keys = "drxfdsjcomwssdbc";
				let enUsername = AES.encrypt(parmas.username, keys);
				let enCode = AES.encrypt(parmas.code, keys);
				let encrypts = {};
				encrypts.username = enUsername;
				encrypts.code = enCode;
				//调用登录接口
				this.goToLogin(encrypts);
			},
			//调用登录接口
			goToLogin(parmas) {
				let that = this;
				this.$api.autoLign(parmas).then(res => {
					//用户信息
					let dataUser = res.data.data.logInfo;
					//token
					let token = res.data.data.loginToken;
					this.$store.commit("SET_USERINFO", dataUser || {});
					this.$store.commit("SET_TOKEN", token || {});
					uni.reLaunch({
						url: '../index/index',
					});
				}).catch((err) => {
					that.$refs.loading.hideLoading()
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			},
			//选项卡点击事件
			swichNav: function(e) {
				this.currentTab = e.target.dataset.current;
			},
			//点击注册
			goToRegist() {
				uni.navigateTo({
					url: "/pages/register/register"
				})
			},
			//点击忘记密码
			goToForget() {
				uni.navigateTo({
					url: "/pages/forgetPassword/forgetPassword"
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #ffffff;
	}

	.login {
		.logo_wrap {
			width: 100%;
			display: flex;
			justify-content: center;
		}

		.logo_wrap .img {
			width: 150rpx;
			height: 150rpx;
			margin: 75rpx 0 20rpx 0;
			border-radius: 7px;
		}

		.content {
			width: 610rpx;
			margin: 0 auto;
		}

		.btn_wrap {
			border-bottom: 1px solid rgba(221, 221, 221, 0.3);
			margin-bottom: 120rpx;
			display: flex;
			justify-content: space-around;
		}

		.header_tab {
			font-size: 14px;
			text-align: center;
			padding: 0.8rem 0.2rem;
			color: #999999;
		}

		.on {
			color: #585cf7 !important;
			border-bottom: 2px solid #585cf7;
		}

		.btn {
			margin-top: 100rpx;
		}

		.loginBtn {
			width: 80%;
			margin: 68rpx 10% 0 10%;
			background: #585CF7;
			border-radius: 25px;
			color: white;
			height: 88rpx;
			line-height: 88rpx;
			font-size: 16px;
		}

		.zc-btn {
			margin-top: 30rpx;
			text-align: center;
		}
	}
	.footer {
	  width: 100%;
	  height: 200rpx;
	  margin-top: 112rpx;
	  text-align: center;
	  color: #999999;
	  font-size: 13px;
	}
	.footer .img {
	  width: 100%;
	  height: 100%;
	  background-repeat: repeat;
	}
	.img-hover {
	  background-color: transparent;  
	}
	.footer .btn {
	  width: 40px;
	  height: 40px;
	  margin-top: 20rpx;
	  padding: 0;
	}
</style>
